package com.wangsd.common.utils;

import com.wangsd.web.sys.entity.SysButton;
import org.springframework.web.util.UriUtils;

import java.util.List;

/**
 * ApplicationUtils : 程序工具类，提供大量的便捷方法
 */
public class ApplicationUtils {


    /**
     * 将16进制的byte数组转换成字符串
     *
     * @param raw
     * @return
     */
    public static String getBufHexStr(byte[] raw) {
        String HEXES = "0123456789ABCDEF";
        if (raw == null) {
            return null;
        }
        final StringBuilder hex = new StringBuilder(2 * raw.length);
        for (final byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
        }
        return hex.toString();
    }

    /**
     * 将16进制的字符串转成字符数组
     *
     * @param str
     * @return
     */
    public static byte[] getHexBytes(String str) {
        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }
        return bytes;
    }

    /**
     * url 编码
     * @param source url
     * @return 编码后的url
     */
    public static String encodeURL(String source) {
        return UriUtils.encode(source, "utf-8");
    }

    public static Long getButtonId(List<SysButton> buttonlist, String action) {
        if (buttonlist != null && buttonlist.size() > 0) {
            for (SysButton button : buttonlist) {
                if (action.equals(button.getAction())) {
                   return button.getId();
                }
            }
        }
        return null;
    }

    /**
     * 获取指定位数的数据数
     * @param length
     * @return String str
     */
    public static String getRandomStr(Integer length) {
        String str = "";
        for(int i = 0;i < length;i++){
            str += (int)(Math.random()*10);
        }
        return str;
    }


    public static void main(String[] args) {
        System.out.println(getRandomStr(1));
    }
}
